import request from '@/utils/request'

// Jenkins构建相关API
export const jenkinsApi = {
  // 获取所有构建记录
  getBuilds: (params?: any) => {
    return request({
      url: '/api/jenkins/jenkins-builds/',
      method: 'get',
      params
    })
  },

  // 获取特定任务的构建记录
  getJobBuilds: (jobId: number) => {
    return request({
      url: `/api/jenkins/jenkins-jobs/${jobId}/builds/`,
      method: 'get'
    })
  },

  // 获取构建详情
  getBuildDetail: (buildId: number) => {
    return request({
      url: `/api/jenkins/jenkins-builds/${buildId}/`,
      method: 'get'
    })
  },

  // 获取构建控制台输出
  getBuildConsole: (jobId: number, buildNumber: number) => {
    return request({
      url: `/api/jenkins/jenkins-jobs/${jobId}/console/${buildNumber}/`,
      method: 'get'
    })
  },

  // 通过构建ID获取控制台输出
  getBuildConsoleById: (buildId: number) => {
    return request({
      url: `/api/jenkins/jenkins-builds/${buildId}/console_output/`,
      method: 'get'
    })
  },

  // 同步构建记录
  syncBuilds: (jobId?: number) => {
    if (jobId) {
      return request({
        url: `/api/jenkins/jenkins-jobs/${jobId}/sync_builds/`,
        method: 'get'
      })
    } else {
      // 同步所有构建记录
      return request({
        url: '/api/jenkins/jenkins-builds/sync_all/',
        method: 'post'
      })
    }
  },

  // 触发构建
  triggerBuild: (jobId: number, parameters?: any) => {
    return request({
      url: `/api/jenkins/jenkins-jobs/${jobId}/trigger_build/`,
      method: 'post',
      data: parameters
    })
  },

  // 停止构建
  stopBuild: (jobId: number, buildNumber: number) => {
    return request({
      url: `/api/jenkins/jenkins-jobs/${jobId}/builds/${buildNumber}/stop/`,
      method: 'post'
    })
  }
}

// Jenkins任务相关API
export const jenkinsJobApi = {
  // 获取任务列表
  getJobs: (params?: any) => {
    return request({
      url: '/api/jenkins/jenkins-jobs/',
      method: 'get',
      params
    })
  },

  // 获取任务详情
  getJobDetail: (jobId: number) => {
    return request({
      url: `/api/jenkins/jenkins-jobs/${jobId}/`,
      method: 'get'
    })
  }
}

// Jenkins服务器相关API
export const jenkinsServerApi = {
  // 获取服务器列表
  getServers: () => {
    return request({
      url: '/api/jenkins/jenkins-servers/',
      method: 'get'
    })
  },

  // 测试服务器连接
  testConnection: (serverId: number) => {
    return request({
      url: `/api/jenkins/jenkins-servers/${serverId}/test_connection/`,
      method: 'post'
    })
  },

  // 创建服务器
  createServer: (data: any) => {
    return request({
      url: '/api/jenkins/jenkins-servers/',
      method: 'post',
      data
    })
  },

  // 更新服务器
  updateServer: (serverId: number, data: any) => {
    return request({
      url: `/api/jenkins/jenkins-servers/${serverId}/`,
      method: 'put',
      data
    })
  },

  // 删除服务器
  deleteServer: (serverId: number) => {
    return request({
      url: `/api/jenkins/jenkins-servers/${serverId}/`,
      method: 'delete'
    })
  },

  // 同步任务
  syncJobs: (serverId: number) => {
    return request({
      url: `/api/jenkins/jenkins-servers/${serverId}/sync_jobs/`,
      method: 'post'
    })
  },

  // 测试所有连接
  testAllConnections: () => {
    return request({
      url: '/api/jenkins/jenkins-servers/test_all_connections/',
      method: 'post'
    })
  }
}

// 项目和环境API（复用现有的）
export const projectApi = {
  getProjects: () => {
    return request({
      url: '/api/jenkins/projects/',
      method: 'get'
    })
  }
}

export const environmentApi = {
  getEnvironments: () => {
    return request({
      url: '/api/jenkins/environments/',
      method: 'get'
    })
  }
}
